﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.Security;
using CoolERP_Common;

namespace CoolERP_Web
{
    [PermissionCheck(false)]
    public partial class Login : BasePage
    {
        private string JumpUrl
        {
            get { return this.ViewState["JumpUrl"] == null ? string.Empty : this.ViewState["JumpUrl"].ToString(); }
            set { this.ViewState["JumpUrl"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request.QueryString["url"] != null)
                JumpUrl = this.Request.QueryString["url"];
        }

        protected void imgLogin_Click(object sender, ImageClickEventArgs e)
        {
            using (UsersBLL uBLL = new UsersBLL())
            {
                hidType.Value = hidType.Value;
                Users user = new Users();
                int result = uBLL.Login(this.tbName.Text.Trim(), MD5Helper.EncryptString(this.tbPassword.Text.Trim()), Convert.ToInt32(hidType.Value), ref user);
                switch (result)
                {
                    case 0:
                        CurrentUser c_user = new CurrentUser();
                        c_user.UserID = user.Id;
                        c_user.UserName = user.Name;
                        c_user.IsAnonymous = false;
                        c_user.PermissionCodes = uBLL.GetUserPermission(user.Id).Select(t => t.Code).ToList();
                        this.currentUser = c_user;
                        //Session.Add("JumpUrl", JumpUrl);
                        Response.Redirect("User/Default.aspx");
                        break;
                    case -1:
                        this.lbResult.Text = "无此用户";
                        //this.AjaxWebJavaScript("setReset();");
                        break;
                    case -2:
                        this.lbResult.Text = "密码不对";
                        //this.AjaxWebJavaScript("setReset();");
                        break;
                }
            }
        }
    }
}